package com.sds.sweng.swat.miplatform.ui.design.editors;

import org.eclipse.core.resources.IResourceChangeEvent;
import org.eclipse.core.resources.IResourceChangeListener;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.dialogs.ErrorDialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IEditorSite;
import org.eclipse.ui.IFileEditorInput;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.part.FileEditorInput;
import org.eclipse.ui.part.MultiPageEditorPart;

public class MiplatformUIDesigner extends MultiPageEditorPart implements IResourceChangeListener {

	private MiplatformDesignEditor designEditor;
	private MiplatformSourceEditor sourceEditor;

	private int DESIGN_PAGE;
	private int SOURCE_PAGE;

	public MiplatformUIDesigner() {
		super();
		ResourcesPlugin.getWorkspace().addResourceChangeListener(this);
	}

	protected void createPages() {
		createDesignPage();
		createSourcePage();
	}

	private void createDesignPage() {
		try {
			designEditor = new MiplatformDesignEditor();
			DESIGN_PAGE = addPage(designEditor, getEditorInput());
			setPageText(DESIGN_PAGE, "Design");
		} catch (PartInitException e) {
			ErrorDialog.openError(
				getSite().getShell(),
				"Error creating nested graphical editor",
				null,
				e.getStatus());
		}
	}

	private void createSourcePage() {
		try {
			sourceEditor = new MiplatformSourceEditor();
			SOURCE_PAGE = addPage(sourceEditor, getEditorInput());
			setPageText(SOURCE_PAGE, "Source");
			setTitle(sourceEditor.getTitle());
		} catch (PartInitException e) {
			ErrorDialog.openError(
				getSite().getShell(),
				"Error creating nested text editor",
				null,
				e.getStatus());
		}
	}

	public void init(IEditorSite site, IEditorInput editorInput) throws PartInitException {
		if (!(editorInput instanceof IFileEditorInput))
			throw new PartInitException("Invalid Input: Must be IFileEditorInput");
		super.init(site, editorInput);
	}

	public void dispose() {
		ResourcesPlugin.getWorkspace().removeResourceChangeListener(this);
		super.dispose();
	}

	public void doSave(IProgressMonitor monitor) {
		getEditor(SOURCE_PAGE).doSave(monitor);
	}

	public void doSaveAs() {
		IEditorPart editor = getEditor(SOURCE_PAGE);
		editor.doSaveAs();
		setTitle(editor.getTitle());
		setInput(editor.getEditorInput());
	}

	public boolean isSaveAsAllowed() {
		return true;
	}

	public void resourceChanged(final IResourceChangeEvent event) {
		switch (event.getType()) {
			case IResourceChangeEvent.PRE_CLOSE:
				Display.getDefault().asyncExec(new Runnable(){
					public void run(){
						IWorkbenchPage[] pages = getSite().getWorkbenchWindow().getPages();
						for (int i = 0; i<pages.length; i++){
							if(((FileEditorInput)sourceEditor.getEditorInput()).getFile().getProject().equals(event.getResource())){
								IEditorPart editorPart = pages[i].findEditor(sourceEditor.getEditorInput());
								pages[i].closeEditor(editorPart,true);
							}
						}
					}
				});
				break;
			default:
				break;
		}
	}

}
